//
//  ViewController.m
//  CALayerMaskObjc
//
//  Created by liy on 2025/1/16.
//

#import "ViewController.h"

@interface ViewController ()
@property (nonatomic, strong) CALayer *imageLayer;
@property (nonatomic, strong) CALayer *maskLayer;

@property (nonatomic, strong) UIImage *imageContents;
@property (nonatomic, strong) UIImage *maskContents;

@end

@implementation ViewController
//https://www.jianshu.com/p/4ead89ddb7b5
- (void)viewDidLoad {
  [super viewDidLoad];

  self.view.backgroundColor = [UIColor grayColor];
  //获取图片
  self.imageContents = [UIImage imageNamed:@"origin"];
  self.maskContents = [UIImage imageNamed:@"mask"];

  //	创建图片layer
  self.imageLayer = [CALayer layer];
  self.imageLayer.frame = self.view.bounds;
  self.imageLayer.contents = (__bridge id _Nullable)(self.imageContents.CGImage);
  [self.view.layer addSublayer:self.imageLayer];

  //创建遮罩layer
  self.maskLayer = [CALayer layer];
  self.maskLayer.frame = CGRectMake(150, 150, 200, 200);
	//设置遮罩圆形
	self.maskLayer.cornerRadius = 100; //圆形
	self.maskLayer.masksToBounds = true;
  self.maskLayer.contents = (__bridge id)self.maskContents.CGImage;

  //给图片layer提供遮罩layer
  self.imageLayer.mask = self.maskLayer;

  //3秒之后做maskLayer动画
  [self performSelector:@selector(maskLayerAnimation) withObject:nil afterDelay:3.f];
}

- (void)maskLayerAnimation {
  self.maskLayer.frame = CGRectMake(150, 300, 200, 200);
}

@end
